package org.itstep.stepup.dal.infrastructure;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class SessionFactoryUtil {
	
	private static SessionFactory factory = null;
	private static Object sync = new Object();
	
	public static SessionFactory getInstance() {
		if (factory == null) {
			synchronized(sync) {
				if (factory == null) {
					factory = createSessionFactory();
				}
			}
		}
		return factory;
	}
	
	private static SessionFactory createSessionFactory() {
		//return new Configuration().configure().buildSessionFactory();
	    Configuration configuration = new Configuration();
	    configuration.configure();
	    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
	    return configuration.buildSessionFactory(serviceRegistry);
	}
	
}
